package com.xdtech.hotel.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.sys.aspect.SystemControllerLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.hotel.service.TestDataService;
import com.xdtech.hotel.vo.TestDataItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-06-05 14:18:20
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class TestDataController {
	private Log log = LogFactory.getLog(TestDataController.class);
	@Autowired
	private TestDataService testDataService;
	@RequestMapping(value="/testData.do",params = "testData")
	public ModelAndView skipTestData() {
		return new ModelAndView("hotel/testData/testData_ftl");
	}
	
	@RequestMapping(value="/testData.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(TestDataItem item,Pagination pg) {
		Map<String, Object> results =  testDataService.loadPageDataByConditions(pg,item,"findTestDataByCondition");
		return results;
	}
	
	@RequestMapping(value="/testData.do",params = "editTestData")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editTestData(HttpServletRequest request,Long testDataId) {
		if (testDataId!=null) {
			request.setAttribute("testDataItem", testDataService.loadTestDataItem(testDataId));
		}
		return new ModelAndView("hotel/testData/editTestData_ftl");
	}
	
	@RequestMapping(value="/testData.do",params = "saveTestData")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveTestData(TestDataItem item) {
		ResultMessage r = new ResultMessage();
		if (testDataService.saveOrUpdateTestData(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/testData.do",params = "deleteTestDataItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteTestDataItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> testDataIds = new ArrayList<Long>();
			for (String id : tempIds) {
				testDataIds.add(Long.valueOf(id));
			}
			testDataService.deleteTestDataInfo(testDataIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
